import com.pbking.component.ComponentBase;
import com.pbking.util.eventHandling.Event;
import com.pbking.logging.Logger;

/**
 * @author Jason Crist
 */
class com.pbking.component.ui.ComponentButton extends ComponentBase {
	
	public var data;
	private var _allowUserInteraction:Boolean = true;
	
	function ComponentButton()
	{
		this.addEventListener("onEnabledChanged", this, onEnabledChanged);
		setMode("up");
	}
	
	public function get allowUserInteraction():Boolean { return this._allowUserInteraction; }
	public function set allowUserInteraction(newVal:Boolean):Void
	{
		this._allowUserInteraction = newVal;
	}
	
	function onRollOver():Void
	{
		if(enabled && allowUserInteraction)
		{
			setMode("over");
			dispatch(new Event("onRollOver", this));
		}
	}
	
	function onRollOut():Void
	{
		if(enabled && allowUserInteraction)
		{
			setMode("up");
			dispatch(new Event("onRollOut", this));
		}
	}
	
	function onPress():Void
	{
		Logger.debug("Entry: ", "ComponentButton", "onPress");
		
		if(enabled && allowUserInteraction)
		{
			setMode("down");
			dispatch(new Event("onPress", this));
		}
	}
	
	function onRelease():Void
	{
		if(enabled && allowUserInteraction)
		{
			setMode("over");
			dispatch(new Event("onRelease", this));
			dispatch(new Event(Event.CLICK, this));
		}
	}
	
	function onReleaseOutside():Void
	{
		if(enabled && allowUserInteraction)
		{
			setMode("up");
			dispatch(new Event("onRelease", this));
		}
	}
	
	private function onEnabledChanged(event:Event):Void
	{
		if(this.enabled)
		{
			if( isMouseOver() )
			{
				setMode("over");
			}
			else
			{
				setMode("up");
			}			
		} 
		else 
		{
			setMode("disabled");			
		}
		
		Logger.debug("Exit: " + _mode, "ComponentButton", "onEnabledChanged");		
	}	
}